package com.example.s.Activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.s.R;
import com.example.s.adapter.InnerHolder;
import com.example.s.adapter.MyRecyclerViewAdapter;
import com.nex3z.flowlayout.FlowLayout;

import java.util.ArrayList;
import java.util.List;

public class SelectLabelActivity extends BaseActivity {
    private ImageView back;
    private TextView finish;
    private FlowLayout selectLabelFl;
private  StringBuilder tagList = new StringBuilder();
    private EditText labelEt;
    private List<String> mLabelList = new ArrayList<>();
    private RecyclerView categoryLabelRv;
    private FlowLayout categoryContentFl;
    private ImageView refresh;
    private List<CagegoryLabel> mCategoryLabelList = new ArrayList<>();
    private MyRecyclerViewAdapter<CagegoryLabel> mCategoryLabelAdapter;
    private List<String> mCategoryContentLabelList = new ArrayList<>();


    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        finish = (TextView) findViewById(R.id.finish);
        labelEt = (EditText) findViewById(R.id.labelEt);
        selectLabelFl = (FlowLayout) findViewById(R.id.selectLabelFl);
        categoryLabelRv = (RecyclerView) findViewById(R.id.categoryLabelRv);
        categoryContentFl = (FlowLayout) findViewById(R.id.categoryContentFl);
        refresh = (ImageView) findViewById(R.id.refresh);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_label);
        initView();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        labelEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND && !"".contentEquals(v.getText())) {
                    addSelectLabel(selectLabelFl, v.getText().toString());
                }
                return false;
            }
        });

        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(tagList.length()==0){
                    Toast.makeText(A, "请选择标签", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("tag",tagList.toString());
                setResult(100,intent);
                finish();
            }
        });
        setCategoryRv();


        mCategoryContentLabelList.add("听课的猫");
        mCategoryContentLabelList.add("猫");
        mCategoryContentLabelList.add("听课");
        mCategoryContentLabelList.add("aaaaaa");
        mCategoryContentLabelList.add("ccccccccccc");
        mCategoryContentLabelList.add("听课cc");
        mCategoryContentLabelList.add("听课");

        addContentLabel(categoryContentFl, mCategoryContentLabelList);
    }

    private void setCategoryRv() {
        mCategoryLabelList.add(new CagegoryLabel("历史", true));
        mCategoryLabelList.add(new CagegoryLabel("推荐", false));
        mCategoryLabelList.add(new CagegoryLabel("原创", false));
        mCategoryLabelList.add(new CagegoryLabel("摄影", false));
        mCategoryLabelList.add(new CagegoryLabel("音乐", false));
        mCategoryLabelList.add(new CagegoryLabel("美食", false));
        mCategoryLabelList.add(new CagegoryLabel("其他", false));

        categoryLabelRv.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        categoryLabelRv.setAdapter(mCategoryLabelAdapter = new MyRecyclerViewAdapter<CagegoryLabel>(mCategoryLabelList, R.layout.rv_label_category_item) {
            @Override
            public void handleData(InnerHolder holder, CagegoryLabel item) {
                holder.setText(R.id.title, item.getTitle());
                View itemView = holder.getView(R.id.item);
                ImageView arrow = holder.getView(R.id.arrow);
                if (item.isSelect()) {
                    itemView.setBackgroundResource(R.drawable.history_label_bg_select);
                    arrow.setImageResource(R.mipmap.production_unfold);
                } else {
                    itemView.setBackgroundResource(R.drawable.history_label_bg);
                    arrow.setImageResource(R.mipmap.right_arrow);
                }
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (CagegoryLabel cagegoryLabel : mCategoryLabelList) {
                            cagegoryLabel.setSelect(false);
                        }
                        mCategoryLabelList.get(holder.getAdapterPosition()).setSelect(true);
                        mCategoryLabelAdapter.notifyDataSetChanged();
                        //切换内容标签

                    }
                });
            }

        });
    }


    private void addSelectLabel(FlowLayout fl, String title) {
        View item = LayoutInflater.from(this).inflate(R.layout.fl_selectlabel_item, null);
        TextView label = item.findViewById(R.id.label);
        label.setText(title);
        item.findViewById(R.id.del).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fl.removeView(item);
            }
        });
        //去重操作
        boolean isAdd = true;
        for (int i = 0; i < fl.getChildCount(); i++) {
            TextView textView = fl.getChildAt(i).findViewById(R.id.label);
            String s = textView.getText().toString();
            if (title.equals(s)) {
                isAdd = false;
                break;
            }
        }
        if (isAdd){
            tagList.append("#"+title);
            fl.addView(item);
        }
    }

    private void addContentLabel(FlowLayout fl, List<String> titles) {
        for (String title : titles) {
            View item = LayoutInflater.from(this).inflate(R.layout.fl_category_content_item, null);
            TextView label = item.findViewById(R.id.label);
            label.setText(title);
            fl.addView(item);
            item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "onClick: " + label.getText().toString());
                    addSelectLabel(selectLabelFl, label.getText().toString());
                }
            });
        }
    }

    class CagegoryLabel {
        private String title;
        private boolean mIsSelect;

        public CagegoryLabel(String title, boolean isSelect) {
            this.title = title;
            this.mIsSelect = isSelect;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public boolean isSelect() {
            return mIsSelect;
        }

        public void setSelect(boolean select) {
            mIsSelect = select;
        }
    }
}
